<?php

class nn{
    public $name="nullnamne";
    public $age="28";
    public $color="red";
    public function jsonSerialize(){
        echo $this->name." ".$this->age;

}
}

$cc=json_encode(new nn);
var_dump($cc);
var_dump(json_decode($cc));


 